package assistant.dao.daoImpl;

import achievement.domain.SzlBook;
import assistant.dao.CourseDao;
import assistant.dao.WishDao;
import assistant.domain.Wish;
import cn.hutool.core.util.StrUtil;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2022/12/17 17:53
 * @Author lihu
 */
public class WishDaoImpl implements WishDao {
    private Connection con;
    private PreparedStatement psmt;
    private ResultSet rs;

    @Override
    public void insert(Wish wish) {
        String sql = "insert into lh_wish\n" +
                "        (student_id, course_id, `order`) \n" +
                "values(?,?,?)";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1, wish.getStudentId());
            psmt.setLong(2, wish.getCourseId());
            psmt.setString(3, wish.getOrder());
            psmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt);
        }

    }

    @Override
    public void updateById(Wish wish) {
        String sql = "update lh_wish set";
        List<Object> param = new ArrayList<>();
        if (wish == null) {
            return;
        } else {
            if (wish.getCourseId() != null) {
                sql += " course_id = ?,";
                param.add(wish.getCourseId());
            }
            if (wish.getStudentId() != null) {
                sql += " student_id= ?,";
                param.add(wish.getStudentId());
            }
            if (wish.getOrder() != null) {
                sql += " order = ?,";
                param.add(wish.getOrder());
            }

        }
        sql = StrUtil.strip(sql, null, ",");
        sql += " where id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for (int i = 0; i < param.size(); i++) {
                psmt.setObject(i + 1, param.get(i));
            }
            psmt.setLong(param.size() + 1, wish.getId());
            psmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt);
        }
    }

    @Override
    public void deleteById(Long id) {
        String sql = "delete from lh_wish where id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1, id);
            psmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt);
        }
    }

    @Override
    public Wish selectById(Long id) {
        String sql = "select * from lh_wish where id = ?";
        Wish wish = new Wish();
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1, id);
            rs = psmt.executeQuery();
            while (rs.next()) {
                wish.setId(rs.getLong(1));
                wish.setStudentId(rs.getLong(2));
                wish.setCourseId(rs.getLong(3));
                wish.setOrder(rs.getString(4));

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt, rs);
        }
        return wish;
    }

    @Override
    public List<Wish> selectList(Wish wish) {
        String sql = "select * from lh_wish where 1=1";
        List<Wish> list = new ArrayList<>();
        List<Object> param = new ArrayList<>();
        if (wish == null) {
            sql = "select * from lh_wish";
        } else {
            if (wish.getId() != null) {
                sql += " and id = ?";
                param.add(wish.getId());
            }
            if (wish.getCourseId() != null) {
                sql += " and course_id = ?";
                param.add(wish.getCourseId());
            }
            if (wish.getStudentId() != null) {
                sql += " and student_id= ?";
                param.add(wish.getStudentId());
            }
            if (wish.getOrder() != null) {
                sql += " and order = ?";
                param.add(wish.getOrder());
            }
        }
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for (int i = 0; i < param.size(); i++) {
                psmt.setObject(i + 1, param.get(i));
            }
            rs = psmt.executeQuery();
            while (rs.next()) {
                Wish wish1 = new Wish();
                wish1.setId(rs.getLong(1));
                wish1.setStudentId(rs.getLong(2));
                wish1.setCourseId(rs.getLong(3));
                wish1.setOrder(rs.getString(4));
                list.add(wish1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt, rs);
        }
        return list;
    }
}
